package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffectType;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class DispelSkill extends BaseSkill {

    public DispelSkill(int level) {
        super(level);
    }

    public DispelSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public DispelSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("驱散：每次清除敌人正面buff。");
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean onAttack(MobEntity attacker, LivingEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        Collection<StatusEffectInstance> targetEffects = target.getStatusEffects();
        List<StatusEffect> statusEffectTypeList = new ArrayList<>();
        AtomicInteger num = new AtomicInteger(getSkillLevel());
        if (!targetEffects.isEmpty()) {
            targetEffects.forEach(statusEffectInstance -> {
                        if (statusEffectInstance.getEffectType().getType() == StatusEffectType.BENEFICIAL) {
                            if (num.get() > 0) {
                                statusEffectTypeList.add(statusEffectInstance.getEffectType());
                                num.getAndDecrement();
                            }
                        }
                    }
            );
            statusEffectTypeList.forEach(target::removeStatusEffect);
        }
        return true;
    }
}
